<?php

/*
**	FORM newCommentForm
*/

$this->form_validation->set_rules('comment', 'Comment', 'trim|required');
$this->form_validation->set_rules('postTwitter', 'Also post my comment on Twitter', 'trim');

if ($this->form_validation->run() == TRUE) {
	
	// Action 'Add comment'
	if ($this->input->post('action:addComment')) {

		$commentId = $this->Ligature_Comment_model->createComment(
				$this->session->userdata('current_user_id'),
				$this->input->post('postId'),
				$this->input->post('comment')
			);
		
		// optionally tweet comment
		if ($this->input->post('postTwitter') == 'accept') {
			
			$post = $this->Ligature_Post_model->readPostBySlug($slug);
			
			$this->Ligature_Comment_model->tweetComment(
					$post->getPostId(),
					$post->getAuthorTwitterScreenName(),
					$commentId,
					$this->input->post('comment')
				);
		}

		$outcome = 'commentSaved';

		switch ($outcome) {
			/*
				Outcome 'Comment saved'
			*/
			case 'commentSaved':
				$messageClass = 'success';
				$this->session->set_flashdata('message', array('class' => $messageClass, 'text' => $this->lang->line('ligature_post_newCommentForm_addComment_commentSaved_message')));
				redirect('/post/' . $slug . '#comment-' . $commentId);
				break;
			default:
				show_error("Unexpected outcome '$outcome' to action 'addComment' in form 'newCommentForm' on page 'post'.");
		}

	}
	else {
		show_error("Unexpected action '${action.id}' in form 'newCommentForm' on page 'post'.");
	}

}

/* clickframes::version=312987160::clickframes */
?>